package com.forum.module.blog.controller.likes;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.favorite.vo.FavoritePageReqVO;
import com.forum.module.blog.controller.favorite.vo.FavoriteRespVO;
import com.forum.module.blog.controller.favorite.vo.FavoriteSavaReqVO;
import com.forum.module.blog.controller.likes.vo.LikesPageReqVO;
import com.forum.module.blog.controller.likes.vo.LikesRespVO;
import com.forum.module.blog.controller.likes.vo.LikesSavaReqVO;
import com.forum.module.blog.convert.favorite.FavoriteConvert;
import com.forum.module.blog.convert.likes.LikesConvert;
import com.forum.module.blog.convert.user.UserConvert;
import com.forum.module.blog.dal.dataobject.favorite.FavoriteDO;
import com.forum.module.blog.dal.dataobject.likes.LikesDO;
import com.forum.module.blog.service.likes.LikesService;
import com.forum.module.blog.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:18
 */
@Tag(name = "博客管理 - 点赞")
@RestController
@RequestMapping("/blog/likes")
public class LikesController {
    private final LikesService likesService;

    private final UserService userService;

    public LikesController(LikesService likesService,
                           UserService userService) {
        this.likesService = likesService;
        this.userService = userService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增点赞")
    public DataResult<Long> createLikes(@RequestBody LikesSavaReqVO reqVO) {
        Long id = likesService.createLikes(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新点赞")
    public DataResult<Boolean> updateLikes(@RequestBody LikesSavaReqVO reqVO) {
        likesService.updateLikes(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点赞信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteLikes(@RequestParam("id") Long id) {
        likesService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询点赞列表")
    public DataResult<PageResult<LikesRespVO>> page(@RequestBody @Valid LikesPageReqVO pageReqVO) {
        // 获得文章分页列表
        PageResult<LikesDO> pageResult = likesService.getLikesPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        List<LikesRespVO> likesRespVOS = pageResult.getList().stream()
                .map(likesDO -> {
                    LikesRespVO likesRespVO = LikesConvert.INSTANCE.covert(likesDO);
                    likesRespVO.setUserInfo(UserConvert.INSTANCE.covert(userService.getById(likesRespVO.getUserId())));
                    return likesRespVO;
                })
                .collect(Collectors.toList());

        return success(new PageResult<>(likesRespVOS, pageResult.getTotal()));
    }
}
